package com.apes.scm.warning;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.pi.service.PiManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

@Service("priceWarningService")
public class PriceWarningService extends DomainService {
    @Autowired
    private PiManager piManager;
    @Autowired
    private PriceWarningDao priceWarningDao;

    /**
     * 查询配件价格预警汇总
     */
    public JSONArray querySummaryForPriceWarning(SimpleRequest request) {
        String method = "scmRclWarningService.querySummaryForPriceWarning";
        return getOldSystemData(method);
    }

    /**
     * 查询配件价格预警汇总
     */
    public JSONArray qryGroupSumPriceWarning(SimpleRequest request) {
        String method = "scmRclWarningService.qryGroupSumPriceWarning";
        return getOldSystemData(method);
    }
    private JSONArray getOldSystemData(String method) {
        JSONObject getResponseData = piManager.synErp(method, new JSONObject());
        JSONObject obj = JSONObject.parseObject(getResponseData.getString("obj"));
        JSONArray item = obj.getJSONArray("item");
        item.stream().forEach(i ->{
            JSONObject price = (JSONObject)i;
            price.put("formId","scm.warning.queryPriceWarningDetail");
        });
        JSONObject crossPriceNum = (JSONObject) priceWarningDao.findAllCrossPriceNum().get(0);
        JSONObject crossPrice =new JSONObject();
        crossPrice.put("LEFTTEXT","异业价格预警");
        crossPrice.put("RIGHTTEXT",crossPriceNum.getIntValue("COUNT"));
        crossPrice.put("formId","scm.warning.findAllCrossPriceItems");

        item.add(crossPrice);
        return item;
    }

    /**
     * 查找配件价格预警明细
     * @param request
     * @return
     */
    public Page<Object> qryPriceWarning(SimpleRequest request) {
        String method = "scmRclWarningService.qryPriceWarning";
        return getOldSystemDetailData(request,method);
    }


    /**
     * 查找工时价格预警明细
     * @param request
     * @return
     */
    public Page<Object> queryPriceWarningDetail(SimpleRequest request) {
        String method = "scmRclWarningService.queryPriceWarningDetail";
        return getOldSystemDetailData(request,method);
    }
    /**
     * 查找异业合作价格预警明细
     * @param request
     * @return
     */
    public Page<Object> findAllCrossPriceItems(SimpleRequest request) {
        JSONObject param = JSONObject.parseObject(request.getData().toString());
        JSONArray itemArray = priceWarningDao.findAllCrossPriceItems();
        JSONObject obj = (JSONObject)itemArray.get(0);
        Pageable pageable = PageRequest.of(((Integer) request.get("page")) - 1, request.get("size"));
        return new PageImpl<Object>(itemArray, pageable, itemArray.size() == 0 ? 0 : obj.size());
    }

    private Page<Object> getOldSystemDetailData(SimpleRequest request,String  method) {
        JSONObject param = JSONObject.parseObject(request.getData().toString());
        JSONObject condi = new JSONObject();
        condi.put("PAGE", param.get("page"));
        condi.put("SIZE", param.get("size"));
        condi.put("LEFTTEXT", param.getJSONObject("data").get("LEFTTEXT"));
        JSONObject getResponseData = piManager.synErp(method, condi);
        JSONObject obj = JSONObject.parseObject(getResponseData.getString("obj"));
        JSONArray itemArray = obj.getJSONArray("item");

        Pageable pageable = PageRequest.of(((Integer) request.get("page")) - 1, request.get("size"));
        return new PageImpl<Object>(itemArray, pageable, itemArray.size() == 0 ? 0 : obj.getInteger("total"));
    }
}
